<template>
  <div class="cover">
    <div class="box-reg">
      <div class="box-litter"></div>
      <el-form :model="regform" :rules="regfromrules" ref="regform">
        <el-form-item prop="username">
          <el-input v-model="regform.username" prefix-icon="el-icon-user-solid" placeholder="请输入账户"> </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="regform.password"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="regform.repassword"
            type="password"
            prefix-icon="el-icon-lock"
            placeholder="请再次输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="zc" @click="regUser">点击注册</el-button>
          <el-link type="info" @click="$router.push('/login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// import axios from 'axios'
import { regApi } from '../api/user.js'
export default {
  name: 'reg',
  // 自定义校验规则
  data() {
    const row = (rule, value, cb) => {
      if (value === this.regform.password) {
        cb()
      } else {
        cb(new Error('两次密码不一致!'))
      }
    }
    return {
      // 数据
      regform: {
        username: '',
        password: '',
        repassword: ''
      },
      // 验证数据规则
      regfromrules: {
        username: [
          // 必填项规则
          { required: true, message: '账户不能为空', trigger: 'blur' },
          { min: 3, max: 7, message: '请输入正确的格式', trigger: 'blur' },
          // 正则验证规则
          { pattern: /^[A-Za-z0-9]{1,10}$/, message: '请输入1到10位的字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '请输入6到15位的非空字符', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '请输入6到15位的非空字符', trigger: 'blur' },
          { validator: row, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 校验文本框是否输入正确,正确则访问请求
    regUser() {
      this.$refs.regform.validate(async Vlied => {
        if (!Vlied) return
        // 验证成功后执行数据提交
        // const { data: res } = await axios.post('http://www.liulongbin.top:3008/api/reg', this.regform)
        const { data: res } = await regApi(this.regform)
        if (res.code !== 0) return this.$message.error(res.message)
        this.$message.success(res.message)
        this.$router.push('/login')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.cover {
  height: 100%;
  background: url('../assets//images/login_bg.jpg') no-repeat;
  background-size: cover;
  .box-reg {
    height: 335px;
    width: 500px;
    background-color: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    .box-litter {
      width: 100%;
      height: 40px;
      background: url('../assets/images/login_title.png') no-repeat;
      background-position: center;
    }
    .zc {
      width: 100%;
    }
  }
}
</style>
